package com.wxd.demo.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wxd.demo.dao.TestDao;
import com.wxd.demo.entity.Test;
import com.wxd.demo.service.TestService;
import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.office.ExcelReader;
import com.wxd.framework.common.office.ExcelWriter;

@Service
public class TestServiceImpl implements TestService {

	@Autowired
	private TestDao testDao;
	
	@Override
	public String getTestValue() {
		Test test = testDao.selectByPrimaryKey(1);
		if (test != null) {
			return test.getTestValue();
		} else {
			return "";
		}
	}

	@Override
	public Map<Boolean, List<Test>> resolveDataFromServerFileForTest(String fileName) throws IOException {
		Map<Boolean, List<Test>> map = new HashMap<>();
		File pathFile = new File(fileName);
		ExcelReader excelReader = new ExcelReader(pathFile);
		try{
			excelReader.open();
		}catch(IOException ex){
			ex.printStackTrace();
			throw ex;
		}
		boolean flag = false;
		List<Test> list = new ArrayList<>();
		
		int startRow = 2;
		int totalSheetCount = excelReader.getSheetCount();
		if (totalSheetCount < 1) {
			return map;
		}
		excelReader.setSheetNum(0);// 设置读取的工作表索引
		int totalCount = excelReader.getRowCount();// 总行数
		for(int i = startRow -1; i <= totalCount; i++){
			String[] cols = excelReader.readExcelLine(i);
			String idStr = cols[0];
			int id = 0;
			if (StringUtils.isNotBlank(idStr)) {
				if (idStr.indexOf(".") > 0) {
					// 正则表达
					idStr = idStr.replaceAll("0+?$", "");// 去掉后面无用的零
					idStr = idStr.replaceAll("[.]$", "");// 如小数点后面全是零则去掉小数点
				}
				if (idStr.indexOf(".") < 0) {
					id = Integer.parseInt(idStr);
				}
			}
			String testValue = cols[1];
			String desc1 = cols[2];
			String desc2 = cols[3];
			String desc3 = cols[4];
			String desc4 = cols[5];
			Test test = new Test();
			test.setId(id);
			test.setTestValue(testValue);
			test.setDesc1(desc1);
			test.setDesc2(desc2);
			test.setDesc3(desc3);
			test.setDesc4(desc4);
			list.add(test);
		}
		if (pathFile.exists()) {
			pathFile.delete();
		}
		map.put(flag, list);
		return map;
	}

	@Override
	public void saveTemplateDataForTest(String[] records) {
		List<Test> list = new ArrayList<>();
		for (String s : records){
			String[] record = s.split(FrameConstants.PAR_FLAG, -1);
			int id = Integer.parseInt(record[0]);
			String testValue = record[1];
			String desc1 = record[2];
			String desc2 = record[3];
			String desc3 = record[4];
			String desc4 = record[5];
			Test test = new Test();
			test.setId(id);
			test.setTestValue(testValue);
			test.setDesc1(desc1);
			test.setDesc2(desc2);
			test.setDesc3(desc3);
			test.setDesc4(desc4);
			list.add(test);
		}
		testDao.insertInBatch(list);
	}

	@Override
	public void exportTestInfo(OutputStream outputStream) throws IOException {
		List<Test> list = testDao.selectAllList();
		ExcelWriter excelWriter = new ExcelWriter(outputStream);
		excelWriter.createRow(0);
		excelWriter.setCell(0, "id");
		excelWriter.setCell(1, "testValue");
		excelWriter.setCell(2, "desc1");
		excelWriter.setCell(3, "desc2");
		excelWriter.setCell(4, "desc3");
		excelWriter.setCell(5, "desc4");
		for (int i = 0; i < list.size(); i++) {
			excelWriter.createRow(i + 1);
			excelWriter.setCell(0, list.get(i).getId());
			excelWriter.setCell(1, list.get(i).getTestValue());
			excelWriter.setCell(2, list.get(i).getDesc1());
			excelWriter.setCell(3, list.get(i).getDesc2());
			excelWriter.setCell(4, list.get(i).getDesc3());
			excelWriter.setCell(5, list.get(i).getDesc4());
		}
		excelWriter.export();		
	}
}
